package com.eairlv.route.client.service;

import com.eairlv.route.client.entity.RouteHandlerForm;
import com.eairlv.route.client.interceptor.RouteInterceptor;
import com.eairlv.route.client.entity.AuthenticationProperties;
import com.eairlv.route.client.entity.RouteForm;
import feign.Target;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Method;

/**
 * @author eairlv
 * @description
 * @date 16:19 2019/12/12
 */
@Slf4j
public class RouteLoadService {

    @Getter
    private RouteClientService routeClientService;

    private RouteInterceptor routeInterceptor;

    public RouteLoadService(RouteClientService routeClientService, RouteInterceptor routeInterceptor) {
        this.routeClientService = routeClientService;
        this.routeInterceptor = routeInterceptor;
    }

    /**
     * 获取路由信息
     * @param key
     * @return
     */
    public AuthenticationProperties getRoute(Object key, Integer type, Method method, Object[] args, Target target){
        return getRoute(routeInterceptor.applyKey(key, type, method, args, target),
                routeInterceptor.applyType(key, type, method, args, target));
    }

    public AuthenticationProperties getRoute(String key, Integer type){
        return routeClientService.getRoute(key, type);
    }

    /**
     * 通过session获取路由信息
     * @param session
     * @return
     */
    public AuthenticationProperties getRouteBySession(String session){
        return routeClientService.getRouteBySession(session);
    }

    /**
     * 设置路由信息
     * @param routeHandlerForm
     * @return
     */
    public Boolean setRoute(RouteHandlerForm routeHandlerForm, Method method, Object[] args, Target target){
        return setRoute(routeInterceptor.applyRoute(routeHandlerForm, method, args, target));
    }

    public Boolean setRoute(RouteForm routeForm){
        return routeClientService.setRoute(routeForm);
    }
}
